﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExamSystem.Mdeol;
using ExamSystem.BLL;
namespace ExamSystem.ht
{
    public partial class TQuestion_Add : System.Web.UI.Page
    {
        private TkechengBLL kb = new TkechengBLL();
        private TquestionBLL tb = new TquestionBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                List<Tkecheng> list = kb.selectkckecheng();
                Tkecheng kt = new Tkecheng(0,"--请选择--");
                list.Insert(0,kt);
                this.tkcId.DataSource = list;
                this.tkcId.DataTextField = "tkcname";
                this.tkcId.DataValueField = "tkcid";
                this.tkcId.DataBind();
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if(string.IsNullOrEmpty(this.tkcId.Value))
                {
                    this.lbMsg.InnerText = "请选择课程";
                    return;
                }
                string tqname = this.tqName.Value;//试题名称
                int tkcid = int.Parse(this.tkcId.Value);
                string tqtype = this.type1.Value; //试题类型
                if(type2.Checked)
                {
                    tqtype = this.type2.Value;
                }
                int tqscore =int.Parse(this.tqScore.Value); //试题分值
                string tqa = this.tqA.Value; //选项A
                string tqb = this.tqB.Value; //选项B
                string tqc = this.tqC.Value; //选项C
                string tqd = this.tqD.Value;//选项D
                string tqanswer = this.tqAnswer.Value.ToUpper();//答案

                //验证
                if (string.IsNullOrEmpty(tqname))
                {
                    this.lbMsg.InnerText = "请输入试题名称";
                    return;
                }
                if (string.IsNullOrEmpty(tqa))
                {
                    this.lbMsg.InnerText = "请输入选项A";
                    return;
                }
                if (string.IsNullOrEmpty(tqb))
                {
                    this.lbMsg.InnerText = "请输入选项B";
                    return;
                }
                if (string.IsNullOrEmpty(tqc))
                {
                    this.lbMsg.InnerText = "请输入选项C";
                    return;
                }
                if (string.IsNullOrEmpty(tqd))
                {
                    this.lbMsg.InnerText = "请输入选项D";
                    return;
                }
                //判断类型和答案是否匹配
                if (tqtype == this.type1.Value)
                {
                    if (tqanswer.Length != 1)
                    {
                        this.lbMsg.InnerText = "单选题，只能选择一个选项";
                        return;
                    }
                }
                else
                {
                    if (tqanswer.Length <= 1)
                    {
                        this.lbMsg.InnerText = "多选题，至少选择两个选项";
                        return;
                    }
                }
                //入库

                Tkecheng tk = new Tkecheng(tkcid, null);
                Tquestion tq = new Tquestion(0, tk,tqname,tqtype,tqscore,tqa,tqb,tqc,tqd,tqanswer);
                int b = tb.insert(tq);
                if (b > 0)
                {
                    util.FunHelper.LayerAlter(this, "添加成功", "TQuestion_List.aspx");
                }
                else
                {
                    util.FunHelper.LayerAlter(this, "添加失败");
                }
            }
            catch (Exception ex)
            {
                util.FunHelper.LayerAlter(this, ex+"");
            }
        }
    }
}